<template>
	<view class="">
		<ncDusease 
			titleName="新增病害" 
			compoType="add" 
			:subTitleName="titleObj.subTitle"
			:lineSiteSectionType="titleObj.lineSiteSectionType"
			v-model="titleObj.mileage"
			@addSubmit="addSubmit"
			textType="提交"
		></ncDusease>
	</view>
</template>

<script>
	import ncDusease from '../../common/ncDusease.vue'
	import { diseaseOrderSave, getLastFillingRecord } from "@/api/engineeringMaintenance/engineeringMaintenance.js"
	import mixins from '@/utils/mixins.js'
	import { BS,BSM } from '@/session/index.js'
	import { doGet, doSave, doDelete } from "@/utils/offline/sqlite/index.js";
	//巡检的所有接口
	import * as XUNJIAN from '@/api/engineeringMaintenance/engineeringMaintenance.js'
	import { setNetworkState, Network_Offline, Network_Online } from '@/utils/offline/config.js'
	import { getPicture, deleteImg, doc_deleteAttachs } from '@/api/doc.js'
	export default {
		name: 'newDiseases',
		components: {
			ncDusease
		},
		mixins:[mixins],
		data(){
			return {
				titleObj:{},
				requestStatus : false
			}
		},
		methods: {
			//新增按钮事件
			async addSubmit(obj) {
				console.log(this.titleObj, '----');
				// setNetworkState(Network_Online)
				console.log(obj, '父组件')
				if (obj && obj.videoFileList && obj.videoFileList.length > 0) {
					const taskArr = obj.videoFileList.filter(it => it.uploadStatus)
					if (taskArr.length > 0) {
						uni.showToast({
							title: '视频上传中,请稍等',
							icon: 'none',
							mask: true
						})
						return
					}
				}


				// let arr = [
				// 	obj.startMileageFront,
				// 	obj.startMileageAfter,
				// 	obj.endMileageFront,
				// 	obj.endMileageAfter
				// ], isTrue2 = arr2.every( it => it), isNull2 = arr2.every(it => !it)
				// if(!isTrue){
				// 	return uni.showToast({
				// 		title:'请检查数据格式',
				// 		icon:'none'
				// 	})
				// }


				// let arr2 = [
				// 	obj.startNum,
				// 	obj.endNum,
				// ], isTrue2 = arr2.every( it => it), isNull2 = arr2.every(it => !it)
				// if(!isTrue2 && !isNull2){
				// 	return uni.showToast({
				// 		title:'请检查环号',
				// 		icon:'none'
				// 	})
				// }
				const {
					result
				} = await doGet(getLastFillingRecord, JSON.parse(uni.getStorageSync('lineData')))
				console.log(result, '填报记录--')
				this.$set(obj, 'orderType', 'add')
				this.$set(obj, 'followup', '新增')
				this.$set(obj, 'createUserName', this.$store.getters.name)
				this.$set(obj, 'createDate', new Date().getTime())
				this.$set(obj, 'inspectUser', result.inspectUser)
				this.$set(obj, 'projectId', uni.getStorageSync('isOwner'))
				const localLine = JSON.parse(uni.getStorageSync('lineData'))
				this.$delete(localLine,'mileage')
				let testObj = Object.assign(obj,localLine)
				testObj = {...testObj,mileage:this.titleObj.mileage}
				// console.log({...testObj}, 'testObj111有啥')
                const hasRing = !!(testObj.startNumber && testObj.endNumber)
                const hasMile = !!(testObj.startMileageFront && testObj.startMileageAfter && testObj.endMileageFront && testObj
                    .endMileageAfter)
                const hasOther = !!(testObj.others)
                const hasStationLocation = !!(testObj.stationLocation)
                if (!hasRing && !hasMile && !hasOther && !hasStationLocation) {
                    if (testObj.lineSiteSectionType == 'qj') {
                        uni.showToast({
                            title: '里程、环号、其他中必须填写一个!',
                            icon: 'none',
                        })
                    } else if (testObj.lineSiteSectionType == 'cz') {
                        uni.showToast({
                            title: '车站位置和其他中必须填写一个!',
                            icon: 'none',
                        })
                    } else {
                        uni.showToast({
                            title: '位置为必填项!',
                            icon: 'none',
                        })
                    }
                    return
                }
                if (!hasRing && (testObj.startNumber || testObj.endNumber)) {
                    return uni.showToast({
                        title:'请检查数据格式',
                        icon:'none'
                    })
                }
                if (!hasMile && (testObj.startMileageFront || testObj.startMileageAfter || testObj.endMileageFront || testObj.endMileageAfter)) {
                    return uni.showToast({
                        title:'请检查数据格式',
                        icon:'none'
                    })
                }
				if(this.requestStatus) return
				this.requestStatus = true
				// 删除视频记录的视频id
				let arr = [...testObj.removeFileSet] || []
				if(arr.length) {
					await doc_deleteAttachs(arr)
				}
				doSave(XUNJIAN.diseaseOrderSave,{...testObj})
				.then(async res => {
					console.log(res, '新增---离线时的展示')
					console.log(res.success, '离线');
					if(res.success) {
						uni.showToast({
							title:'成功！',
							icon:'none',
							mask:true,
						})
						setTimeout(() => {
							uni.navigateBack()
							this.requestStatus = false
						},1600)

					}else {
						this.requestStatus = false
					}
				})
				// setNetworkState(Network_Online)
			}
		},
		onLoad(params) {
			if (params && params.titleParams) {
				this.titleObj = JSON.parse(params.titleParams)
				console.log(this.titleObj, 'title66666');
			}
		},
	}
</script>

<style lang="scss" scoped>

</style>